package com.love.poetry.interfaces.security;

import static org.springframework.web.bind.annotation.RequestMethod. DELETE ;
import static org.springframework.web.bind.annotation.RequestMethod. GET ;
import static org.springframework.web.bind.annotation.RequestMethod. POST ;
import static org.springframework.web.bind.annotation.RequestMethod. PUT ;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.love.poetry.application.security.AuthorityService;
import com.love.poetry.domain.security.Authority;
import com.youboy.core.orm.Page;
import com.youboy.core.web.controller.CrudControllerSupport;
import com.youboy.util.EntityUtils;

@Controller
@RequestMapping ("/security/authority" )
public class SecurityAuthorityController extends CrudControllerSupport<String, Authority> {

             private static final String REDIRECT_LIST = "redirect:/security/authority/list" ;

             @Autowired
             private AuthorityService authorityService ;
             
             @Override
             protected String getViewPackage() {
                         return "security/authority" ;
            }
             
             @RequestMapping (value = "/list" , method = GET)
             public String list(Page<Authority> page, Model model) {
                        page = authorityService .queryPage(page);
                        model.addAttribute(page);
                         return listView();
            }

             @Override
             @RequestMapping (value = "/create" , method = GET)
             public String create(Model model) {
                        model.addAttribute( new Authority());
                         return formView();
            }

             @Override
             @RequestMapping (value = "/create" , method = POST)
             public String create(Authority entity, BindingResult result) {
                         if (result.hasErrors()) {
                                     return null ;
                        }

                        entity.create();
                        success( "用户权限创建成功" );
                         return REDIRECT_LIST ;
            }

             @Override
             @RequestMapping (value = "/{id}/edit" , method = GET)
             public String edit(@PathVariable ( "id") String id, Model model) {
                        Authority entity = authorityService .get(id);
                        model.addAttribute(entity).addAttribute( "_method" , "PUT" );
                         return formView();
            }

             @Override
             @RequestMapping (value = "/{id}/edit" , method = PUT)
             public String edit(@PathVariable ( "id") String id, HttpServletRequest request) {
                         try {

                                    Authority entity = authorityService .get(id);
                                    bind(request, entity);
                                    entity.modify();
                                    success( "用户权限修改成功" );
                        } catch (Exception e) {
                                     return null ;
                        }

                         return REDIRECT_LIST ;
            }

             @Override
             @RequestMapping (value = "/{id}/delete" , method = DELETE)
             public String delete(@PathVariable ( "id") String id) {
                         authorityService .get(id).delete();
                         return REDIRECT_LIST ;
            }

             @Override
             @RequestMapping (value = "/delete" , method = DELETE)
             public String delete(HttpServletRequest request) {
                        String[] items = request.getParameterValues( "items" );
                         for (String item : EntityUtils.nullSafe(items, new String[] {})) {
                                    delete(item);
                        }

                         return REDIRECT_LIST ;
            }
}